package org.jeecg.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.domain.dto.PriceDto;
import org.jeecg.domain.dto.ServeDto;
import org.jeecg.domain.po.Region;
import org.jeecg.domain.po.Serve;
import org.jeecg.domain.vo.ServeVO;
import org.jeecg.mapper.RegionMapper;
import org.jeecg.service.ServeService;
import org.jeecg.mapper.ServeMapper;
import org.jeecg.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
* @author 32327
* @description 针对表【serve(服务表)】的数据库操作Service实现
* @createDate 2024-12-20 09:54:32
*/
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve>
    implements ServeService{
    @Autowired
    private ServeMapper serveMapper;
    @Autowired
    private RegionMapper regionMapper;
    /*
     *fzy
     *根据区域id查询服务列表(分页)
     */
    @Override
    public ResponseResult getServeListByRegionId(ServeDto dto) {
//        利用分页
        Page<ServeVO> page = new Page<>(dto.getPageSum(), dto.getPageSize());
//        查询所有
        List<ServeVO> serveListByRegionId = serveMapper.getServeListByRegionId(page, dto);
//        for (ServeVO serveVO : serveListByRegionId) {
//            System.out.println(serveVO);
//        }
//        将分页数据设置到page中
        page.setRecords(serveListByRegionId);
//        page.setTotal(serveListByRegionId.size());
//        打印
//        for (ServeVO record : page.getRecords()) {
//            System.out.println(record);
//        }
        return ResponseResult.okResult(page);
    }
    /*
     *fzy
     *根据id删除（逻辑删除）
     */
    @Override
    public ResponseResult deleteById(Long id) {
//        逻辑删除
        boolean b = this.removeById(id);
        return b?ResponseResult.okResult("删除成功"):ResponseResult.errorResult(500,"删除失败");
    }
    /*
     *fzy
     * 根据服务id设置热门/取消热门
     */
    @Override
    public ResponseResult setHotById(Long id) {
//        先根据id查询服务是否是热门
        Serve one = this.getOne(Wrappers.<Serve>lambdaQuery().eq(Serve::getId, id));
//        是否为热门，0非热门，1热门
        if(one.getIsHot()==1){
//            改成非热门
            one.setIsHot(0);
        }else{
//            改成热门
            one.setIsHot(1);
        }
        boolean b = this.updateById(one);
        return b?ResponseResult.okResult("设置成功"):ResponseResult.errorResult(500,"设置失败");
    }
    /*
     *fzy
     * 根据服务id设置上架/下架
     */
    @Override
    public ResponseResult setSaleStatus(Long id) {
        //        先根据id查询服务是否上架
        Serve one = this.getOne(Wrappers.<Serve>lambdaQuery().eq(Serve::getId, id));
        System.out.println(one);
//        售卖状态，0：草稿，1下架，2上架
        if(one.getSaleStatus()==1){
//            改成下架
            one.setSaleStatus(2);
        }else if (one.getSaleStatus()==2){
//            改成上架
            one.setSaleStatus(1);
        }else if (one.getSaleStatus()==0){
//            如果是草稿，改成上架
            one.setSaleStatus(2);
        }
        boolean b = this.updateById(one);
        return b?ResponseResult.okResult("修改成功"):ResponseResult.errorResult(500,"修改失败");
    }
    //    添加服务
    @Override
    public ResponseResult saveServe(Serve serve) {
//        初始添加是草稿0
        serve.setSaleStatus(0);
//        初始添加是非热度0
        serve.setIsHot(0);
//        根据区域id查询城市编码
        Region region = regionMapper.selectRegionById(serve.getRegionId());
        serve.setCityCode(region.getCityCode());
        boolean b = this.save(serve);
        return b?ResponseResult.okResult("添加成功"):ResponseResult.errorResult(500,"添加失败");
    }
    //    修改区间价格
    @Override
    public ResponseResult updatePrice(PriceDto dto) {
//        根据id来查询服务
        Serve one = this.getOne(Wrappers.<Serve>lambdaQuery().eq(Serve::getId, dto.getId()));
//        给服务设置要修改的区间价格
        one.setPrice(dto.getPrice());
        boolean b = this.updateById(one);
        return b?ResponseResult.okResult("修改成功"):ResponseResult.errorResult(500,"修改失败");
    }
}




